package concurrency.reentrantLock;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

@Slf4j
public class ReentrantLockTest2_tryLock {

    private static ReentrantLock lock = new ReentrantLock();

    public static void main(String[] args) {
        //可超时
        Thread t1 = new Thread(() -> {
            try {
                if (!lock.tryLock(1, TimeUnit.SECONDS)){
                    log.debug("等了1秒没获得锁，返回");
                    return;
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            try{
                log.debug("拿到锁了");
            }finally{
                lock.unlock();
            }
        }, "reentrantlock可超时测试线程");

        lock.lock();
        log.debug("获取到锁了");
        t1.start();
        try {
            Thread.sleep(2000);
        } catch(InterruptedException e){
            e.printStackTrace();
        } finally
        {
            lock.unlock();
        }
    }


}
